package com.xyz.musicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import beans.User;
import utils.DBAdapter;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    //声明控件
    EditText editTextForNewAccount,editTextForNewUsername,editTextForNewPasswd;
    Button buttonCancel,buttonConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
    }

    private void init(){
        //实例化控件
        editTextForNewAccount = (EditText) findViewById(R.id.et_acount);
        editTextForNewUsername = (EditText) findViewById(R.id.et_username);
        editTextForNewPasswd = (EditText) findViewById(R.id.et_passwd);
        buttonCancel = (Button) findViewById(R.id.bt_cancel);
        buttonConfirm = (Button) findViewById(R.id.bt_confirm);

        //事件监听
        buttonCancel.setOnClickListener(this);
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //数据库链接开启
                DBAdapter dbAdapter = new DBAdapter(RegisterActivity.this);
                dbAdapter.open();
                //获取输入
                String newAccount = editTextForNewAccount.getText().toString();
                String newUsername = editTextForNewUsername.getText().toString();
                String newPasswd = editTextForNewPasswd.getText().toString();

                //将信息封装到对象中
                User newUser = new User(newAccount,newUsername,newPasswd);

                long insertOK = dbAdapter.insert(newUser);

                if ( insertOK == -1 )
                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                else{
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch ( v.getId() ){
            case R.id.bt_cancel:{
                finish();
            }break;
            default:
                break;
        }
    }


}